package logic

import (
	"context"
	"fmt"
	"taobao/models/mysql"
	"taobao/pkg"

	"taobao/taobaorpc/internal/svc"
	"taobao/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *taobaorpc.RegisterRequest) (*taobaorpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("当前账号未登录，请前去注册")
	}
	use := mysql.User{
		Account:  in.Account,
		Password: pkg.Md5(in.Password),
	}
	mysql.DB.Debug().Create(&use)
	return &taobaorpc.RegisterResponse{
		UserId: int64(use.ID),
	}, nil
}
